/*
 * 
 */
package ged.diagram.edit.parts;

import ged.GedPackage;
import ged.Node;
import ged.customizationCode.functions.CustomizationCodeNodeEditPart;
import ged.customizationCode.functions.LabelPositioning;
import ged.customizationCode.shapes.EllipseNodeFigure;
import ged.customizationCode.shapes.PolygonNodeFigure;
import ged.customizationCode.shapes.RectangleNodeFigure;
import ged.diagram.edit.policies.NodeItemSemanticEditPolicy;
import ged.diagram.part.GedDiagramEditorPlugin;
import ged.diagram.part.GedVisualIDRegistry;
import ged.diagram.providers.GedElementTypes;
import ged.extension.point.definition.handlers.EvaluateCintributionsForImages;
import ged.extension.point.definition.handlers.EvaluateContributionsHandler;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.handles.MoveHandle;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderedShapeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IBorderItemEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.BorderItemSelectionEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.figures.BorderItemLocator;
import org.eclipse.gmf.runtime.diagram.ui.figures.BorderedNodeFigure;
import org.eclipse.gmf.runtime.draw2d.ui.figures.ConstrainedToolbarLayout;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.draw2d.ui.mapmode.IMapMode;
import org.eclipse.gmf.runtime.draw2d.ui.render.figures.ScalableImageFigure;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.graphics.Color;
import org.osgi.framework.Bundle;

/**
 * @generated NOT
 */
public class NodeEditPart extends AbstractBorderedShapeEditPart {//extends ShapeNodeEditPart

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 2001;

	/**
	 * @generated
	 */
	protected IFigure contentPane;

	/**
	 * @generated
	 */
	protected IFigure primaryShape;

	protected org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure figure;
	//create a new instance of the class with the methods used for customisation of the editor
	CustomizationCodeNodeEditPart customizationCodeNodeEditPart = new CustomizationCodeNodeEditPart();
	IMapMode mapMode;
	Node node;
	//This value is returned when a custom shape has been given by the user and the shape is available
	WrappingLabel custom_shape_wrapping_label = null;

	/**
	 * @generated
	 */
	public NodeEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new NodeItemSemanticEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, createLayoutEditPolicy());
		// XXX need an SCR to runtime to have another abstract superclass that would let children add reasonable editpolicies
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.CONNECTION_HANDLES_ROLE);
	}

	/**
	 * @generated NOT
	 */
	protected LayoutEditPolicy createLayoutEditPolicy() {
		org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy lep = new org.eclipse.gmf.runtime.diagram.ui.editpolicies.LayoutEditPolicy() {

			protected EditPolicy createChildEditPolicy(EditPart child) {
				Node node = (Node) resolveSemanticElement();
				ged.Label_attributes label_attributes = node
						.getLabel_attributes();
				if (label_attributes != null) {
					ged.LabelPlacement placementOfLabel = label_attributes
							.getLabel_placement();
					String placementOfLabelToString = placementOfLabel
							.toString();
					if (placementOfLabelToString.equals("external")) {
						View childView = (View) child.getModel();// --
						switch (ged.diagram.part.GedVisualIDRegistry
								.getVisualID(childView)) {
						case ged.diagram.edit.parts.NodeLabelEditPart.VISUAL_ID: {
							BorderItemSelectionEditPolicy ep = new BorderItemSelectionEditPolicy() {

								protected List createSelectionHandles() {
									MoveHandle mh = new MoveHandle(
											(GraphicalEditPart) getHost());
									mh.setBorder(null);
									return Collections.singletonList(mh);
								}
							};
							ep.setDragAllowed(true);
							return ep;
						}

						//return new NonResizableLabelEditPolicy();
						}// --
					} else {
						EditPolicy result = child
								.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
						if (result == null) {
							result = new NonResizableEditPolicy();
						}
						return result;
					}
				}
				EditPolicy result = child
						.getEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE);
				if (result == null) {
					result = new NonResizableEditPolicy();
				}
				return result;
			}

			protected Command getMoveChildrenCommand(Request request) {
				return null;
			}

			protected Command getCreateCommand(CreateRequest request) {
				return null;
			}
		};
		return lep;
	}

	/**
	 * @generated NOT
	 */
	protected IFigure createNodeShape() {
		Node node = (Node) resolveSemanticElement();
		mapMode = getMapMode();
		Bundle bundle = GedDiagramEditorPlugin.getInstance().getBundle();

		String customShape = node.getCustomShape();
		String customImage = node.getCustomImage();
		ged.NodeImage theImage = node.getImages();
		if (customImage == null)//bug
			customImage = "";
		if (!customImage.equals("")) {
			Map mapImages = new HashMap<String, ScalableImageFigure>();
			EvaluateCintributionsForImages instanceImage = EvaluateCintributionsForImages
					.getInstance();
			mapImages = instanceImage.evaluateContibutedImages();
			//Move next key and value of Map by iterator
			Iterator<Entry<String, ScalableImageFigure>> Images_iterator = mapImages
					.entrySet().iterator();
			String Image_Key = "";
			ScalableImageFigure Image_value = null;
			while (Images_iterator.hasNext()) {
				Map.Entry Images_pairs = (Map.Entry) Images_iterator.next();
				Image_Key = (String) Images_pairs.getKey();
				if (Image_Key.equals(customImage)) {
					Image_value = (ScalableImageFigure) Images_pairs.getValue();
					if (Image_value != null)
						return primaryShape = Image_value;
				}
			}
		}
		//if an image from the available images has been selected.
		if (theImage != null) {
			URL url = customizationCodeNodeEditPart.setSelectedImage(theImage,
					bundle);
			if (url != null) {
				return primaryShape = new ScalableImageFigure(
						org.eclipse.gmf.runtime.draw2d.ui.render.factory.RenderedImageFactory
								.getInstance(url), true, true, true);
			}

		}
		//if the user has give the name of a Class
		if (customShape == null)//bug
			customShape = "";
		if (customShape.equals("")) {
			ged.NodesShape theShape = node.getShape();
			if (theShape != null) {
				switch (theShape) {
				case ROUNDED_RECTANGLE:
					primaryShape = new NodeFigure();
					break;
				case RECTANGLE:
					primaryShape = new RectangleNodeFigure(node, mapMode);
					break;
				case ELLIPSE:
					primaryShape = new EllipseNodeFigure(node, mapMode);
					break;
				case POLYGON:
					primaryShape = new PolygonNodeFigure(node, mapMode);
				}
				return primaryShape;
			}
		} else//if a custom shape has been given
		{
			Map map = new HashMap<String, Map<IFigure, WrappingLabel>>();
			EvaluateContributionsHandler instance = EvaluateContributionsHandler
					.getInstance();
			map = instance.evaluate(node, mapMode);

			//Move next key and value of Map by iterator
			Iterator<Entry<String, Map<IFigure, WrappingLabel>>> iterator = map
					.entrySet().iterator();
			int i = 0;
			String key = "";
			Map<IFigure, WrappingLabel> value = null;
			while (iterator.hasNext() && i == 0) {
				Map.Entry pairs = (Map.Entry) iterator.next();
				key = (String) pairs.getKey();
				value = (Map<IFigure, WrappingLabel>) pairs.getValue();
				iterator.remove(); 
				//if there is an extension for the customShape
				if (key.equals(customShape)) {
					i = 1;//end loop
					if (value != null) {
						custom_shape_wrapping_label = value.entrySet()
								.iterator().next().getValue();
						return primaryShape = value.entrySet().iterator()
								.next().getKey();
					}
				}
			}
		}
		refresh();
		return primaryShape = new NodeFigure();
	}

	/**
	 * @generated NOT
	 */
	//this method was changed in order to return IFigure instead for NodeFigure
	public IFigure getPrimaryShape() {
		return primaryShape;
	}

	protected void addBorderItem(IFigure borderItemContainer,
			IBorderItemEditPart borderItemEditPart) {
		if (borderItemEditPart instanceof ged.diagram.edit.parts.NodeLabelEditPart) {
			Node node = (Node) resolveSemanticElement();
			int selectedPosition;
			ged.Label_attributes label_attributes = node.getLabel_attributes();
			if (label_attributes != null) {
				selectedPosition = customizationCodeNodeEditPart
						.setPositionOfLabel(label_attributes);
			} else
				selectedPosition = PositionConstants.SOUTH;
			BorderItemLocator locator = new BorderItemLocator(getMainFigure(),
					selectedPosition) {
				@Override
				protected Point locateOnParent(Point suggestedLocation,
						int suggestedSide, IFigure borderItem) {
					return suggestedLocation;
				}

				@Override
				public Rectangle getValidLocation(Rectangle proposedLocation,
						IFigure borderItem) {
					return proposedLocation;
				}
			};
			locator.setBorderItemOffset(new Dimension(-5, -5));
			borderItemContainer.add(borderItemEditPart.getFigure(), locator);
		} else {
			super.addBorderItem(borderItemContainer, borderItemEditPart);
		}
	}

	/**
	 * @generated NOT
	 */
	protected boolean addFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof NodeLabelEditPart) {
			//Take the semantic element
			Node node = (Node) resolveSemanticElement();
			//depending on the type of the primaryShape different actions are performed
			if (getPrimaryShape() instanceof NodeFigure) {
				WrappingLabel labelOfFigure = ((NodeFigure) getPrimaryShape())
						.getFigureNodeLabelFigure();
				customizationCodeNodeEditPart.setLabelFont(labelOfFigure, node);
				customizationCodeNodeEditPart.setFontColor(labelOfFigure, node);
				((ged.diagram.edit.parts.NodeLabelEditPart) childEditPart)
						.setLabel(labelOfFigure);
			} else if (getPrimaryShape() instanceof RectangleNodeFigure) {
				WrappingLabel labelOfFigure = ((RectangleNodeFigure) getPrimaryShape())
						.getFigureNodeLabelFigure();
				customizationCodeNodeEditPart.setLabelFont(labelOfFigure, node);
				customizationCodeNodeEditPart.setFontColor(labelOfFigure, node);
				((ged.diagram.edit.parts.NodeLabelEditPart) childEditPart)
						.setLabel(labelOfFigure);
			} else if (getPrimaryShape() instanceof EllipseNodeFigure) {
				WrappingLabel labelOfFigure = ((EllipseNodeFigure) getPrimaryShape())
						.getFigureNodeLabelFigure();
				customizationCodeNodeEditPart.setLabelFont(labelOfFigure, node);
				customizationCodeNodeEditPart.setFontColor(labelOfFigure, node);
				((ged.diagram.edit.parts.NodeLabelEditPart) childEditPart)
						.setLabel(labelOfFigure);
			} else if (getPrimaryShape() instanceof PolygonNodeFigure) {
				WrappingLabel labelOfFigure = ((PolygonNodeFigure) getPrimaryShape())
						.getFigureNodeLabelFigure();
				customizationCodeNodeEditPart.setLabelFont(labelOfFigure, node);
				customizationCodeNodeEditPart.setFontColor(labelOfFigure, node);
				((ged.diagram.edit.parts.NodeLabelEditPart) childEditPart)
						.setLabel(labelOfFigure);
			} else if (custom_shape_wrapping_label != null) {
				customizationCodeNodeEditPart.setLabelFont(
						custom_shape_wrapping_label, node);
				customizationCodeNodeEditPart.setFontColor(
						custom_shape_wrapping_label, node);
				((ged.diagram.edit.parts.NodeLabelEditPart) childEditPart)
						.setLabel(custom_shape_wrapping_label);
			}
			return true;
		}
		return false;
	}

	/**
	 * @generated
	 */
	protected boolean removeFixedChild(EditPart childEditPart) {
		if (childEditPart instanceof NodeLabelEditPart) {
			return true;
		}
		return false;
	}

	/**
	 * @generated NOT
	 */
	protected void addChildVisual(EditPart childEditPart, int index) {
		Node node = (Node) resolveSemanticElement();
		ged.Label_attributes label_attributes = node.getLabel_attributes();
		if (label_attributes != null) {
			ged.LabelPlacement placementOfLabel = label_attributes
					.getLabel_placement();
			switch (placementOfLabel) {
			case INTERNAL:
				if (addFixedChild(childEditPart)) {
					return;
				}
				super.addChildVisual(childEditPart, 10);
				break;
			case EXTERNAL:
				//with the getFigure we want to create a label from the NodeLabelEditPart and set it as the label
				IFigure childFigure = ((NodeLabelEditPart) childEditPart)
						.getFigure();
				if (childEditPart instanceof IBorderItemEditPart) {
					IFigure borderItemContainer = getContentPaneFor((IGraphicalEditPart) childEditPart);
					addBorderItem(borderItemContainer,
							(IBorderItemEditPart) childEditPart);
				} else {
					IFigure parent = getContentPaneFor((IGraphicalEditPart) childEditPart);

					index = Math.min(parent.getChildren().size(), index);

					parent.add(childFigure, index);
				}
				break;
			}
		} else {
			//default code
			if (addFixedChild(childEditPart)) {
				return;
			}
			super.addChildVisual(childEditPart, -1);
		}
	}

	/**
	 * @generated
	 */
	protected void removeChildVisual(EditPart childEditPart) {
		if (removeFixedChild(childEditPart)) {
			return;
		}
		super.removeChildVisual(childEditPart);
	}

	/**
	 * @generated NOT
	 */
	protected org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure createNodePlate() {
		DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(40, 40); //{
		/*		public PointList getPolygonPoints() {
		 PointList points = new PointList(6);
		 Rectangle anchRect = getHandleBounds();
		 points.addPoint(anchRect.x,                          anchRect.y);                         // A1
		 points.addPoint(anchRect.x + anchRect.width,         anchRect.y);                         // A2
		 points.addPoint(anchRect.x + anchRect.width,         anchRect.y + anchRect.height*30/40); // A3
		 points.addPoint(anchRect.x + anchRect.width * 30/40, anchRect.y + anchRect.height);       // A4
		 points.addPoint(anchRect.x,                          anchRect.y + anchRect.height);       // A5
		 points.addPoint(anchRect.x,                          anchRect.y);                         // A1
		 return points;
		 }
		 };*/

		//UseCaseNodeFigure ucFigure = new UseCaseNodeFigure(getMapMode().DPtoLP(80), getMapMode().DPtoLP(40));

		return result;
	}

	/**
	 * Creates figure for this edit part.
	 * 
	 * Body of this method does not depend on settings in generation model
	 * so you may safely remove <i>generated</i> tag and modify it.
	 * 
	 * @generated NOT
	 */
	protected org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure createNodeFigure() {
		//org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure 
		Node node = (Node) resolveSemanticElement();
		ged.Label_attributes label_attributes = node.getLabel_attributes();
		if (label_attributes != null) {
			ged.LabelPlacement palecementOfLabel = label_attributes
					.getLabel_placement();
			String palecementOfLabelToString = palecementOfLabel.toString();
			if (palecementOfLabelToString.equals("external")) {
				figure = new BorderedNodeFigure(createMainFigure());
				return figure;
			}
		}
		createMainFigure();

		return figure;
	}

	/**
	 * @generated NOT
	 */
	protected org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure createMainFigure() {
		figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		contentPane = setupContentPane(shape);

		Node node = (Node) resolveSemanticElement();

		//set background colour
		customizationCodeNodeEditPart.setBackGround_Color(figure, node);

		//set foreground colour
		customizationCodeNodeEditPart.setForeGround_Color(figure, node);

		//customizationCodeNodeEditPart.setFont_Color

		//sets the margin for the figure. The nodeFigure is placed inner in the figure
		customizationCodeNodeEditPart
				.setOuterMargin(figure, getMapMode(), node);

		//set the width of the line of the border
		//customizationCodeNodeEditPart.setLine_Width();//Do this or use the CustomizationCodeNodeEditPart to separate the code
		ged.LineWidth lineWidth = node.getLinewidth();
		if (lineWidth != null)//-------------
			setLineWidth(lineWidth.getLwidth());

		//set the type of the line of the border
		int type = customizationCodeNodeEditPart.getCurrentBorderLineType(node);
		setLineType(type);

		//set the colour of the line of the border

		//ToolTip
		customizationCodeNodeEditPart.setToolTip(figure, node);

		return figure;
	}

	/**
	 * Default implementation treats passed figure as content pane.
	 * Respects layout one may have set for generated figure.
	 * @param nodeShape instance of generated figure class
	 * @generated NOT
	 */
	protected IFigure setupContentPane(IFigure nodeShape) {
		Node node = (Node) resolveSemanticElement();
		ged.Label_attributes label_attributes = node.getLabel_attributes();
		if (label_attributes != null) {
			ged.LabelPlacement placementOfLabel = label_attributes
					.getLabel_placement();
			String placementOfLabelToString = placementOfLabel.toString();
			//different code sets the position for external labels
			if (placementOfLabelToString.equals("external")) {
				return nodeShape;
			}
			//if the labels are internal call the function getLayout from the labelPositioning class to get the chosen layout
			else {
				LabelPositioning labelPositioning = new LabelPositioning();
				if (nodeShape.getLayoutManager() == null) {
					StackLayout layout = labelPositioning
							.getLayout(label_attributes);
					nodeShape.setLayoutManager(layout);
				}
				return nodeShape;
			}
		}
		if (nodeShape.getLayoutManager() == null) {
			ConstrainedToolbarLayout layout = new ConstrainedToolbarLayout();
			layout.setSpacing(5);
			nodeShape.setLayoutManager(layout);
		}
		return nodeShape; // use nodeShape itself as contentPane
	}

	/**
	 * @generated NOT
	 */
	public IFigure getContentPane() {
		if (contentPane != null) {
			return contentPane;
		}
		return super.getContentPane();
	}

	/**
	 * @generated NOT
	 */
	protected IFigure getContentPaneFor(IGraphicalEditPart editPart) {
		if (editPart instanceof IBorderItemEditPart) {
			return getBorderedFigure().getBorderItemContainer();
			/*} else {
				return getMainFigure();*/
		}
		return getContentPane();
	}

	/**
	 * @generated NOT
	 */
	protected void setForegroundColor(Color color) {
		/*if (primaryShape != null) {
			primaryShape.setForegroundColor(color);
		}*/
	}

	/**
	 * @generated NOT
	 */
	protected void setBackgroundColor(Color color) {
		/*if (primaryShape != null) {
			primaryShape.setBackgroundColor(color);
		}*/
	}

	/**
	 * @generated
	 */
	protected void setLineWidth(int width) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineWidth(width);
		}
	}

	/**
	 * @generated
	 */
	protected void setLineType(int style) {
		if (primaryShape instanceof Shape) {
			((Shape) primaryShape).setLineStyle(style);
		}
	}

	/**
	 * @generated
	 */
	public EditPart getPrimaryChildEditPart() {
		return getChildBySemanticHint(GedVisualIDRegistry
				.getType(NodeLabelEditPart.VISUAL_ID));
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMARelTypesOnSource() {
		ArrayList<IElementType> types = new ArrayList<IElementType>(1);
		types.add(GedElementTypes.Link_4001);
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMARelTypesOnSourceAndTarget(
			IGraphicalEditPart targetEditPart) {
		LinkedList<IElementType> types = new LinkedList<IElementType>();
		if (targetEditPart instanceof ged.diagram.edit.parts.NodeEditPart) {
			types.add(GedElementTypes.Link_4001);
		}
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMATypesForTarget(IElementType relationshipType) {
		LinkedList<IElementType> types = new LinkedList<IElementType>();
		if (relationshipType == GedElementTypes.Link_4001) {
			types.add(GedElementTypes.Node_2001);
		}
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMARelTypesOnTarget() {
		ArrayList<IElementType> types = new ArrayList<IElementType>(1);
		types.add(GedElementTypes.Link_4001);
		return types;
	}

	/**
	 * @generated
	 */
	public List<IElementType> getMATypesForSource(IElementType relationshipType) {
		LinkedList<IElementType> types = new LinkedList<IElementType>();
		if (relationshipType == GedElementTypes.Link_4001) {
			types.add(GedElementTypes.Node_2001);
		}
		return types;
	}

	/**
	 * @generated NOT
	 */
	protected void handleNotificationEvent(Notification event) {
		/**
		 * code for replacing the node - images
		 * identify when a notification comes for a set of the Type structure feature 
		 */
		if (event.getEventType() == Notification.SET
				&& event.getFeatureID(GedPackage.class) == GedPackage.NODE__IMAGES) {
			getFigure().remove(primaryShape);
			primaryShape = createNodeShape();
			getFigure().add(primaryShape);
			this.getFigure().invalidate();
		} else if (event.getNotifier() == getModel()
				&& EcorePackage.eINSTANCE.getEModelElement_EAnnotations()
						.equals(event.getFeature())) {
			handleMajorSemanticChange();
		} else {
			super.handleNotificationEvent(event);
		}
	}

	/**
	 * @generated NOT
	 */
	public class NodeFigure extends RoundedRectangle {

		/**
		 * @generated NOT
		 */
		private WrappingLabel fFigureNodeLabelFigure;

		//private ConnectionAnchor connectionAnchor;

		/**
		 * @generated NOT
		 */
		//constructor
		public NodeFigure() {
			//Take the semantic element
			Node node = (Node) resolveSemanticElement();
			this.setCornerDimensions(new Dimension(getMapMode().DPtoLP(8),
					getMapMode().DPtoLP(8)));

			ged.BorderInnerMargin b_InnerMargin = node.getInnerMargin();
			//sets the margin inside the node. 
			//default code
			if (b_InnerMargin == null) {
				this.setBorder(new MarginBorder(getMapMode().DPtoLP(5),
						getMapMode().DPtoLP(5), getMapMode().DPtoLP(5),
						getMapMode().DPtoLP(5)));
			} else {
				//new code ----
				this.setBorder(new MarginBorder(getMapMode().DPtoLP(
						b_InnerMargin.getInner_margin_top()), getMapMode()
						.DPtoLP(b_InnerMargin.getInner_margin_left()),
						getMapMode().DPtoLP(
								b_InnerMargin.getInner_margin_bottom()),
						getMapMode().DPtoLP(
								b_InnerMargin.getInner_margin_right())));
			}
			createContents();
		}

		/**
		 * @generated NOT
		 */
		private void createContents() {
			fFigureNodeLabelFigure = new WrappingLabel();
			fFigureNodeLabelFigure.setText("Node");

			//methods in the NodeLabelEditPart were commented(refreshVisuals() method)
			this.add(fFigureNodeLabelFigure);
		}

		/**
		 * @generated
		 */
		public WrappingLabel getFigureNodeLabelFigure() {
			return fFigureNodeLabelFigure;
		}

		/**
		 * @generated NOT
		 */
		//Overrides the fillShape method. There is such a method in the Rounded rectangle class
		//add gradient. fillXXX for fill colour and drawXXX for the outline of the shape
		protected void fillShape(Graphics graphics) {
			//Take the semantic element
			Node node = (Node) resolveSemanticElement();
			ged.Gradient fontName = node.getGradient();
			switch (fontName) {
			case VERTICAL:
				//value = "vertical";
				graphics.fillGradient(getBounds(), true);
				break;
			case HORIZONTAL:
				//value = "horizontal";
				graphics.fillGradient(getBounds(), false);
				break;
			default:
				super.fillShape(graphics);//calls the default fillShape method from the RoundedRectangle class
			}

			//-------------------------------PROBLEM---------------------------------------
			//graphics.fillRoundRectangle(getBounds(), corner.width, corner.height);
			//fillGradient method uses the foreground and background colours to set the gradient.
			//getBounds method is in the figure class and returns the bounds for a rectangle not the actual shape
			//thus, the node with the gradient seems to be like a rectangle

		}
	}
}
